package com.heihei.itextdemo.dto;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
  *    响应结果
  *
  */
@ApiModel("响应结果")
public class CommonResponseDTO<T> {
    @ApiModelProperty(value="状态码",dataType = "Integer",notes = "200为正常，其他均为异常")
    private Integer code;
    @ApiModelProperty(value="返回信息",dataType = "String")
    private String message;
    @ApiModelProperty(value="返回值",notes = "根据具体业务返回不同类型值")
    private T data;


    /** 通用错误提示信息 */
    public static final CommonResponseDTO<String> FAILURE = CommonResponseDTO.forFail(400, "警告");
    public static final CommonResponseDTO<String> NOT_LOGIN = CommonResponseDTO.forFail(401, "未登录");
    public static final CommonResponseDTO<String> LOGIN_FAIL = CommonResponseDTO.forFail(402, "用户名或者密码错误");
    public static final CommonResponseDTO<String> LOGIN_TOKEN_ISVALID = CommonResponseDTO.forFail(403, "token无效");
    public static final CommonResponseDTO<String> SYSTEM_ERROR = CommonResponseDTO.forFail(500, "系统错误");
    public static final CommonResponseDTO<String> PARAM_LESS_ERROR = CommonResponseDTO.forFail(600, "缺少参数");

    public static final CommonResponseDTO<String> PARAM_UNVALID = CommonResponseDTO.forFail(618, "非法参数");

    public static final CommonResponseDTO<String> ID_IS_NULL = CommonResponseDTO.forFail(700, "主键不能为空");
    public static final CommonResponseDTO<String> RESOURCE_NOT_EXIST = CommonResponseDTO.forFail(701, "未找到记录");
    public static final CommonResponseDTO<String> AUDITING_CANT_UPDATE = CommonResponseDTO.forFail(700, "审核中，无法修改");
    public static final CommonResponseDTO<String> AUDITING_CANT_DELETE = CommonResponseDTO.forFail(700, "审核中，无法删除");

    /** 记录不存在或已被删除提示信息 */
    public static final CommonResponseDTO<String> RECORD_NO_EXIST_OR_DELETED = CommonResponseDTO.forFail(702, "记录不存在或已被删除");
    public static final CommonResponseDTO<String> RECORD_EXIST = CommonResponseDTO.forFail(703, "记录已存在");
    /**
     * 开发中如需新增返回CODE,需按照功能模块划分，例如用户相关100xx、设备相关101xx，输入对应编码，提示信息即可。如下所示
     */
    public static CommonResponseDTO<String> USER_NOT_EXIST = CommonResponseDTO.forFail(10001, "用户不存在");
    public static CommonResponseDTO<String> USER_EXIST = CommonResponseDTO.forFail(10002, "用户已存在");
    public static CommonResponseDTO<String> ROLE_CANT_DELETE = CommonResponseDTO.forFail(10001, "该角色已和用户绑定，无法删除！");
    public static CommonResponseDTO<String> MOBILE_EXIST = CommonResponseDTO.forFail(10003, "该手机号已存在");
    public static CommonResponseDTO<String> EMAIL_EXIST = CommonResponseDTO.forFail(10004, "该邮箱已存在");

    /** 资产模块 */
    public static CommonResponseDTO<String> STORAGE_CANT_DELETE = CommonResponseDTO.forFail(10501, "仓库仍有物资，不能删除！");

    /** 文件格式  **/
    public static CommonResponseDTO<String> FILE_FORMAT = CommonResponseDTO.forFail(10601, "上传文件格式不正确！");
    public static CommonResponseDTO<String> RELATION_MODEL_EXIST = CommonResponseDTO.forFail(10602, "已关联模型！");

    /** 设备维修计划模块 */
    public static CommonResponseDTO<String> MAINTENANCEPLAN_NOT_EXIST = CommonResponseDTO.forFail(10701, "维修计划不存在！");
    public static CommonResponseDTO<String> MAINTENANCEPLAN_DISPATCHING_NOT_EXIST = CommonResponseDTO.forFail(10702, "派工记录不存在！");

    /** 安全质量模块通用增删改错误返回信息 **/
    public static CommonResponseDTO<String> OBJECT_INSERT_ERROR = CommonResponseDTO.forFail(10801, "添加失败！");
    public static CommonResponseDTO<String> OBJECT_UPDTE_ERROR = CommonResponseDTO.forFail(10802, "更新失败！");
    public static CommonResponseDTO<String> OBJECT_DELETE_ERROR = CommonResponseDTO.forFail(10803, "删除失败！");

    /** 工单模块 */
    public static CommonResponseDTO<String> WORK_ORDER_NOT_EXIST = CommonResponseDTO.forFail(10901, "工单不存在！");
    public static CommonResponseDTO<String> WORK_ORDER_DISPATCHING_NOT_EXIST = CommonResponseDTO.forFail(10902, "派工记录不存在！");


    
    
    
    public CommonResponseDTO(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResponseDTO(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResponseDTO() {

    }

    public static <T> CommonResponseDTO<T> forSuccess(T data) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.SUCCEED, "操作成功");
        success.data = data;
        return success;
    }

    public static <T> CommonResponseDTO<T> forSuccess() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.SUCCEED);
        return success;
    }

    public static <T> CommonResponseDTO<T> forFail() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.FAILED);
        return success;
    }

    public static <T> CommonResponseDTO<T> loginError() {
        CommonResponseDTO<T> error = new CommonResponseDTO<T>(CommonResponseEnum.LOGIN_ERROR.getCode(), CommonResponseEnum.LOGIN_ERROR.getMessage());
        return error;
    }

    public static <T> CommonResponseDTO<T> forFail(String msg) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.FAILED, msg);
        return success;
    }

    public static <T> CommonResponseDTO<T> forException() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.EXCEPTION);
        return success;
    }

    public static <T> CommonResponseDTO<T> forException(String msg) {
        CommonResponseDTO<T> exception = new CommonResponseDTO<T>();
        exception.setResponse(CommonResponseEnum.EXCEPTION, msg);
        return exception;
    }

    public static <T> CommonResponseDTO<T> tokenNotValid() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.TOKEN_NOT_VALID);
        return success;
    }

    public static <T> CommonResponseDTO<T> emptyResult() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.SUCCEED, "无数据");
        return success;
    }

    public static <T> CommonResponseDTO<T> wrongOperate(String msg) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.WRONG_OPERATE, msg);
        return success;
    }

    public static <T> CommonResponseDTO<T> recordNotExists() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.RECORD_NOT_EXIST, "记录不存在");
        return success;
    }

    public static <T> CommonResponseDTO<T> systemError() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.FAILED, "系统错误");
        return success;
    }

    public static <T> CommonResponseDTO<T> paramLess() {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(CommonResponseEnum.MISSING_PARAMS);
        return success;
    }

    public static <T> CommonResponseDTO<T> paramIllegal(String msg) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.message = msg;
        success.code = CommonResponseEnum.ILLEGAL_PARAM.getCode();
        return success;
    }

    public static <T> CommonResponseDTO<T> paramLess(String msg) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.message = msg;
        success.code = CommonResponseEnum.MISSING_PARAMS.getCode();
        return success;
    }

    public static <T> CommonResponseDTO<T> forFail(Integer code, String msg) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.message = msg;
        success.code = code;
        return success;
    }

    public static <T> CommonResponseDTO<T> forFailOrError(CommonResponseEnum responseEnum) {
        CommonResponseDTO<T> success = new CommonResponseDTO<T>();
        success.setResponse(responseEnum);
        return success;
    }

    public boolean success() {
        return code != null && code.equals(CommonResponseEnum.SUCCEED.getCode());
    }

    public CommonResponseDTO<T> setResponse(CommonResponseEnum response, String... vars) {
        this.code = response.getCode();
        this.message = response.getMessage();

        if (vars != null && vars.length > 0) {
            this.message = vars[0];
        }
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}